﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;


public partial class TruckManage_DriverList : System.Web.UI.Page
{
    IBNDriverHandler driverManager = new BNDriverHandler();
    DataBase db = new DataBase();

    private static bool isUpdate = false;
    private static bool isDel = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1042"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    //判断用户有没有添加目的地权限
                    if (strPermission.Contains("1043"))
                    {
                        lblDriverAdd.Visible = true;
                    }
                    else
                    {
                        lblDriverAdd.Visible = false;
                    }
                    //判断用户有没有修改目的地权限
                    if (strPermission.Contains("1044"))
                    {
                        isUpdate = true;
                    }
                    else
                    {
                        isUpdate = false;
                    }
                    //判断用户有没有删除目的地权限
                    if (strPermission.Contains("1045"))
                    {
                        isDel = true;
                    }
                    else
                    {
                        isDel = false;
                    }
                }

               // CheckPagePermission();//判断当前用户有没有添加删除更新的权限

            }
           
            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }
  


    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";
        if (ViewState["Code"] != null)
        {
            list.Add(new SqlParameter("@Code", ViewState["Code"].ToString()));
            sqlstr += " and Code like @Code";
        }
        if (ViewState["Status"] != null)
        {
            list.Add(new SqlParameter("@Status", ViewState["Status"].ToString()));
            sqlstr += " and Status = @Status";
        }

        parms = list.ToArray();
    }

    private void dataBind()
    {
        string clientId = Session["LoginUserClientID"].ToString();
        string sqlStr = "SELECT * FROM BN_Driver where ClientID=" + (clientId == null || clientId.Trim() == "" ? "0" : clientId) + " ";
        SqlParameter[] parms = null;
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dt = driverManager.GetList(sqlStr, parms);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvDriverList.DataSource = view;
        gvDriverList.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }
    }

    //protected void GoPage_TextChanged(object sender, EventArgs e)
    //{
    //    int i = Convert.ToInt32(((DropDownList)sender).Text) - 1;
    //    if (i >= 0 && i < gvMarketList.PageCount)
    //    {
    //        this.gvMarketList.PageIndex = i;
    //        //数据绑定
    //        dataBind();
    //    }
    //}

    //public int[] CreatSource(int numFrom, int numTo)
    //{
    //    int num = numTo - numFrom + 1;
    //    int[] a = new int[num];
    //    for (int i = 0; i < num; i++) { a[i] = i + numFrom; }
    //    return a;
    //}

    protected void gvDriverList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDriverList.PageIndex = e.NewPageIndex;
        dataBind();

    }
    protected void gvDriverList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!driverManager.DeleteByID(int.Parse(e.CommandArgument.ToString())))
            {
                Jscript.AlertInAjax("删除记录失败!", UpdatePanel1);
            }
        }
        dataBind();

    }
    protected void gvDriverList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtCode.Text != string.Empty)
        {
            ViewState["Code"] = txtCode.Text.Trim();
        }
        else
        {
            ViewState["Code"] = null;
        }
        if (radYes.Checked)
        {
            ViewState["Status"] = "1";
        }
        else if (radNo.Checked)
        {
            ViewState["Status"] = "0";
        }
        else
        {
            ViewState["Status"] = null;
        }
        dataBind();
    }

    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvDriverList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvDriverList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");
            Label lblEdit = (Label)e.Row.FindControl("lblUpdate");

            if (isUpdate)
            {
                lblEdit.Visible = true;
            }
            else
            {
                lblEdit.Visible = false;
            }

            if (isDel)
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }
        }
    }

    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvDriverList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvDriverList.PageCount)
            {
                this.gvDriverList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax("请输入大于0小于总页数的正整数!", UpdatePanel1);
        }
    }
}
